<template>
  <div class="deviceClass">
    <c-title :hide="false" :text="pageTitle"></c-title>
    <div class="topHead">
      <div class="txt">当前设备：{{ deviceName }}</div>
    </div>
    <div class="classBox">
      <div class="classBox_left">
        <div class="line" @click="tapNavLeft(index)" v-for="(item, index) in action" :key="index">
          <div class="txt" :class="active == index ? 'act' : ''">{{ item.name }}</div>
        </div>
      </div>
      <div class="classBox_right">
        <template v-if="active == '1'">
          <div class="coupon_lis_shop" :class="select == index ? 'selt' : ''" @click="select = index" v-for="(item, index) in list.has_many_options" :key="index">
            <div class="coupon_lis_left">{{ item.title }}</div>
            <div class="coupon_lis_right">
              售价<span>￥{{ item.product_price }}</span>
            </div>
          </div>
        </template>
        <template v-else-if="active == '2'">
          <div class="coupon_lis" v-for="(item, index) in list" :key="index">
            <div class="coupon_lis_left" v-if="item.coupon">
              <div class="title">{{ item.coupon.title }}</div>
              <div class="time">
                时长：<span class="txt">{{ item.coupon.minute }}分钟</span>
              </div>
            </div>
            <div class="coupon_lis_right" @click="useCoupon(item)" v-if="item.coupon" :class="[item.status != 0 ? 'disable' : '']">{{ item.status != 0 ? "已使用" : "使用" }}</div>
          </div>
        </template>
        <template v-else>
          <div class="lis" v-for="(item, index) in list" :key="index" @click="toGoods(item.goods_id, item.id)">
            <div class="img">
              <img v-if="item.goods" :src="item.goods.thumb" alt="" />
            </div>
            <div class="detail">
              <div class="title" v-if="item.goods">{{ item.goods.title }}</div>
              <div class="detail_bottom">
                <div class="detail_bottom_left">
                  <div class="cargo">货道：{{ item.name }}</div>
                  <div class="price" v-if="item.goods"><span>￥</span>{{ item.goods.price }}</div>
                </div>
                <div class="detail_bottom_right">
                  <i class="iconfont icon-gouwuche2"></i>
                </div>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
    <div class="payBtn set-pc-style" v-if="active == '1'" @click="to_Order">
      <div class="btn">确认支付</div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      active: "0",
      select: 0,
      list: [],
      pageTitle: "商品列表",
      action: ["酒柜商品", "时长套餐商品", "时长券列表"],
      deviceName: "",

      page: 1,
      isLoadMore: true,
      last_page: 1,
      current_page: 1
    };
  },
  activated() {
    this.init();
    this.getData();
  },
  methods: {
    to_Order() {
      console.log(this.select, this.list.has_many_options[this.select], "ddddddddddd");
      let submitActionTag = "-2"; // 立即购买
      let _total = 1;
      let { goods_id, id } = this.list.has_many_options[this.select];
      this.$router.push(
        this.fun.getUrl(
          "goodsorder",
          {},
          {
            tag: submitActionTag,
            goodsId: goods_id,
            optionsId: id,
            total: _total,
            device_id: this.$route.params.device_id,
            is_gradevin_sing:'1'
          }
        )
      );
    },
    toGoods(goods_id, cargoId) {
      this.$router.push(this.fun.getUrl("goods", { id: goods_id }, { device_id: this.$route.params.device_id, cargo_road_id: cargoId }));
    },
    tapNavLeft(ind) {
      let urlstr = "";
      if (ind == "2") {
        this.pageTitle = "时长券列表";
        urlstr = "plugin.gradevin.frontend.consumer.coupon.index";
      } else if (ind == "1") {
        // 时长券商品
        this.pageTitle = "商品列表";
        urlstr = "plugin.gradevin.frontend.consumer.duration.package";
      } else {
        this.pageTitle = "商品列表";
        urlstr = "plugin.gradevin.frontend.consumer.category.goods";
      }
      this.active = ind;
      this.list = [];
      this.select = 0;
      this.page = 1;
      let json = {
        device_id: this.$route.params.device_id,
        page: this.page
      };
      if(this.$route.query.member_coupon_id){
        json.member_coupon_id = this.$route.query.member_coupon_id;
      }
      this.resGetData(urlstr, json);
    },
    getData() {
      let json = {
        device_id: this.$route.params.device_id,
        page: this.page
      };
      if(this.$route.query.member_coupon_id){
        json.member_coupon_id = this.$route.query.member_coupon_id;
      }
      let urlstr = "plugin.gradevin.frontend.consumer.category.goods";
      this.resGetData(urlstr, json);
    },
    init() {
      let json = { device_id: this.$route.params.device_id };
      if(this.$route.query.member_coupon_id){
        json.member_coupon_id = this.$route.query.member_coupon_id;
      }
      $http.post("plugin.gradevin.frontend.consumer.category.index", json, "loading").then(response => {
        if (response.result == 1) {
          this.action = response.data.action;
          this.deviceName = response.data.current_device.name;
          this.active = 0;
        } else {
          this.$toast(response.msg);
        }
      });
    },
    resGetData(urlStr, json) {
      $http.post(urlStr, json, "loading").then(response => {
        if (response.result == 1) {
          console.log(response.data, this.active);
          if (this.active == "2") {
            this.list = response.data.record.data;
            this.total_page = response.data.record.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
          } else {
            this.list = response.data;
          }
        } else {
          this.$toast(response.msg);
        }
      });
    },
    // 使用时长券
    useCoupon(item) {
      if (item.status) {
        return;
      }
      let that = this;
      this.$dialog
        .confirm({ message: `是否确认使用该时长券`, confirmButtonText: "确认", cancelButtonText: "取消" })
        .then(() => {
          that.useCouponPost(item);
        })
        .catch(() => {});
    },
    useCouponPost(item) {
      $http.post("plugin.gradevin.frontend.consumer.coupon.use", { id: item.id, device_id: this.$route.params.device_id }, "loading").then(response => {
        if (response.result == 1) {
          this.$toast("使用成功");
          this.page = 1;
          this.list = [];
          this.total_page = 0;
          this.tapNavLeft(this.active);
        } else {
          this.$toast(response.msg);
        }
      });
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      let json = {
        device_id: this.$route.params.device_id,
        page: this.page
      };
      if (this.page >= this.total_page || this.active != "2") {
        this.isLoadMore = false;
        return;
      } else {
        this.isLoading = true;
        this.page = this.page + 1;
        json.page = this.page;
        $http
          .get("plugin.gradevin.frontend.consumer.coupon.index", json, "加载中")
          .then(response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              this.list = this.list.concat(response.data.record.data);
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              return;
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.deviceClass {
  min-height: 100vh;
  background: #fff;
}
.topHead {
  text-align: left;
  .txt {
    padding: 1rem 0.47rem;
  }
}
.payBtn {
  position: fixed;
  bottom: 1.34rem;
  left: 0;
  width: 100%;
  height: 2.5rem;
  .btn {
    height: 2.5rem;
    margin: 0 0.75rem;
    border-radius: 1.28rem;
    background: #f15353;
    text-align: center;
    line-height: 2.5rem;
    font-size: 0.88rem;
    color: #fff;
  }
}
.classBox {
  display: flex;
  justify-content: space-between;
  .classBox_left {
    position: sticky;
    top: 3rem;
    left: 0;
    width: 5rem;
    height: 90vh;
    .line {
      font-size: 0.81rem;
      line-height: 1rem;
      padding: 1.28rem 0;
      box-sizing: border-box;
      text-align: center;
      .txt {
        padding: 0 0.47rem;
        position: relative;
      }
      .act {
        color: #f15353;
      }
      .act::before {
        content: "";
        position: absolute;
        top: 0;
        width: 0.19rem;
        height: 100%;
        background: #f15353;
        border-radius: 0.94rem;
        left: 0;
      }
    }
    // .act {
    //   position: absolute;
    //   top: 1rem;
    //   width: 0.19rem;
    //   height: 1.25rem;
    //   background: #f15353;
    //   border-radius: 0.94rem;
    // }
  }

  .van-list {
    flex: 1;
  }
  .classBox_right {
    flex: 1;
    padding: 0.78rem 0.69rem 3rem 0.75rem;

    .lis {
      display: flex;
      justify-content: space-between;
      margin-bottom: 0.94rem;
      .img {
        flex-shrink: 0;
        width: 5rem;
        height: 5rem;
        border-radius: 0.5rem;
        margin-right: 0.5rem;
        img {
          width: 100%;
          height: 100%;
          border-radius: 0.5rem;
        }
      }
      .detail {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .title {
          font-size: 0.88rem;
          font-weight: 500;
          line-height: 1rem;
          text-align: left;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .detail_bottom {
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
          .detail_bottom_left {
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            align-items: flex-start;
            .cargo {
              font-size: 0.75rem;
              color: #6e6e79;
              line-height: 0.85rem;
            }
            .price {
              font-size: 1rem;
              font-weight: 500;
              color: #f15353;
              margin-top: 0.5rem;
              line-height: 1rem;
              span {
                font-size: 0.7rem;
              }
            }
          }
          .detail_bottom_right {
            width: 1.5rem;
            height: 1.5rem;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f15353;
            .icon-gouwuche2 {
              font-size: 0.9rem;
              color: #fff;
            }
          }
        }
      }
    }
    .coupon_lis {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 1rem 0.75rem;
      box-sizing: border-box;
      background: #f8f8fb;
      margin-bottom: 0.94rem;
      border-radius: 0.25rem;
      .coupon_lis_left {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        .title {
          font-size: 0.88rem;
          font-weight: 500;
          line-height: 1.1rem;
          margin-bottom: 0.94rem;
        }
        .time {
          font-size: 0.75rem;
          color: #6e6e79;
          .txt {
            font-size: 1rem;
            color: #f15353;
          }
        }
      }
      .disable {
        background: #d6d6dc !important;
      }
      .coupon_lis_right {
        font-size: 0.81rem;
        line-height: 1.38rem;
        flex-shrink: 0;
        width: 4.25rem;
        padding: 0.59rem 0;
        background: #f15353;
        border-radius: 1.88rem;
        color: #fff;
        line-height: 1rem;
      }
    }
    .coupon_lis_shop {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 1rem 0.75rem;
      box-sizing: border-box;
      border-radius: 0.25rem;
      margin-bottom: 0.94rem;
      overflow: hidden;
      .coupon_lis_left {
        font-size: 0.88rem;
        color: #3b3b4a;
      }
      .coupon_lis_right {
        font-size: 0.75rem;
        color: #6e6e79;
        span {
          font-size: 1rem;
          font-weight: 500;
          color: #f15353;
        }
      }
    }
    .selt {
      border: 1px solid #f15353;
      background: #feeded;
    }
  }
}
</style>
